package com.huabo.fxgl.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hbfk.util.JsonBean;
import com.huabo.fxgl.entity.*;
import com.huabo.fxgl.service.IRiskAssplanRiskService;
import com.huabo.fxgl.service.IRiskAssplanService;
import com.huabo.fxgl.service.IRiskLevelmappingService;
import com.huabo.fxgl.service.IRiskcategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.net.URLEncoder;
import java.util.*;

@RestController
//@RequestMapping("/pggl")
@Api(tags = "风险评估 - 评估结果")
@Slf4j
public class RiskAssResultController {

    @Autowired
    private IRiskAssplanService riskAssplanService;


    /**
     * 风险管控 - 风险评估 - 评估结果
     *
     * @param pageNumber 页码
     * @param find       页面查找公共类
     * @return
     * @author wanghongtuo
     * @data 2022/8/2
     * @version 1.0.1
     * @Param limit 每页显示几条
     * @Param organization
     */
    @RequestMapping(value = "/fxxt/fxcl/riresultquerylist")
    @ApiOperation("风险管控 - 风险评估 - 评估结果 - 列表显示 /fxxt/fxcl/riresultquerylist")
    public JsonBean resultQueryList(
            @ApiParam(name = "find", value = "find") Find find,
            @ApiParam(name = "choiceSearch", value = "choiceSearch") String choiceSearch,
            @ApiParam(name = "pageNumber", value = "pageNumber") @RequestParam(defaultValue = "1") Integer pageNumber,
            @ApiParam(name = "organization", value = "organization") Organization organization,
            @ApiParam(name = "limit", value = "limit") @RequestParam(defaultValue = "15") Integer limit) {

        JsonBean jsonBean = new JsonBean();
        jsonBean.setCode(200);
        Map result = new HashMap();

        if (choiceSearch == null || "".equals(choiceSearch)) {
            choiceSearch = "hide";
        }

        result.put("pageBean", riskAssplanService.findRiskAssPlanPageDetails(new Page(pageNumber, limit), find));
        result.put("choiceSearch", choiceSearch);
        result.put("find", find);
        jsonBean.setData(result);
        return jsonBean;

    }

    /**
     * 风险管控 - 风险评估 - 评估结果
     *评估结果热图列表页
     * @author wanghongtuo
     * @data 2022/8/5
     * @version 1.0.1
     * @param pageSize 每页显示几条
     * @param pageNumber 页码
     */
    @Autowired
    private IRiskcategoryService iRiskcategoryService;
    @Autowired
    private IRiskAssplanRiskService riskAssplanRiskService;

    @RequestMapping(value = "/fxxt/fxcl/pgjg_rt_list")
    @ApiOperation("风险管控 - 风险评估 - 评估结果 - 热图数据返回 /fxxt/fxcl/pgjg_rt_list")
    public JsonBean evaluationResultThermographyList(@ApiParam(name = "riskcatid", value = "riskcatid") @RequestParam(required = false) BigDecimal riskcatid,
                                                     @ApiParam(name = "planId", value = "planId") @RequestParam(required = false) BigDecimal planId,
                                                     @ApiParam(name = "choiceSearch", value = "choiceSearch") @RequestParam(required = false) String choiceSearch,
                                                     @ApiParam(name = "pageNumber", value = "pageNumber") @RequestParam(defaultValue = "1") Integer pageNumber,
                                                     @ApiParam(name = "catids", value = "catids") @RequestParam(required = false) String catids,
                                                     @ApiParam(name = "pageSize", value = "pageSize") @RequestParam(defaultValue = "15") Integer pageSize) {

        if (riskcatid != null && !riskcatid.toString().equals("0")) {
            List<Riskcategory> list = iRiskcategoryService.findRiskCateByRoot(riskcatid);
            StringBuilder sb = new StringBuilder("");
            for (int i = 0; i < list.size(); i++) {
                sb.append(list.get(i).getRiskcatid());
                if (i<list.size()-1)
                    sb.append(",");
            }
            catids = sb.toString();
        } else {
            catids = iRiskcategoryService.findRiskCateParentByAssPanid(planId.toString())[1];
            return defaultDisplay(catids, catids, planId, choiceSearch);
        }
        List<RiskAssplanRisk> assPlanRisks = riskAssplanRiskService.findRiskByRiskType(catids, planId);
        String riskids = "";

        if (assPlanRisks.size() > 0) {
            StringBuilder sb = new StringBuilder("");
            for (int i=0; i<assPlanRisks.size(); i++) {
                sb.append(assPlanRisks.get(i).getRisk().getRiskid());
                if (i<assPlanRisks.size()-1)
                    sb.append(",");
            }
            riskids = sb.toString();
        }
        IPage pageBean = new Page(pageNumber, pageSize);
        JsonBean jsonBean = new JsonBean();
        jsonBean.setCode(200);
        Map result = new HashMap();
        if (StringUtils.isNotBlank(riskids) && null != planId) {
            pageBean = this.riskAssplanRiskService.findRiskInRiskIdAndAssId(riskids, planId, pageBean);
        }
        result.put("riskcatid", riskcatid);
        try {
            result.put("url", "/fxxt/fxcl/pgjg_rt_list?catids=" + URLEncoder.encode(catids, "utf-8") + "&planId=" + planId);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        if (choiceSearch == null || "".equals(choiceSearch)) {
            choiceSearch = "hide";
        }
        result.put("choiceSearch", choiceSearch);
        result.put("pageBean", pageBean);
        jsonBean.setData(result);
        return jsonBean;
    }

    /**
     * @author wanghongtuo
     * @Date 2022/8/8
     * @Des:默认显示图
     * @version 1.0.1
     */
    @Autowired
    private IRiskLevelmappingService riskLevelmappingService;

    @RequestMapping(value = "/fxxt/fxcl/pgjg_list_rt")
//    @ApiOperation("风险管控 - 风险评估 - 评估结果 - 热图数据返回 /fxxt/fxcl/pgjg_list_rt")
    public JsonBean defaultDisplay(
            @ApiParam(name = "riskcatid", value = "riskcatid") @RequestParam(required = false) String riskcatid,
            @ApiParam(name = "ids", value = "ids") @RequestParam(required = false) String ids,
            @ApiParam(name = "planId", value = "planId") @RequestParam(required = false) BigDecimal planId,
            @ApiParam(name = "choiceSearch", value = "choiceSearch") @RequestParam(required = false) String choiceSearch) {
        JsonBean jsonBean = new JsonBean();
        jsonBean.setCode(200);
        Map result = riskLevelmappingService.forCycle(riskcatid,planId);
            if (choiceSearch == null || "".equals(choiceSearch)) {
                choiceSearch = "hide";
            }
            result.put("choiceSearch", choiceSearch);
        jsonBean.setData(result);
        return jsonBean;
    }
}